package cappuccino.couldbook.ui;

import android.content.Intent;
import android.support.design.widget.TextInputLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;

import cappuccino.couldbook.Constants;
import cappuccino.couldbook.R;
import cappuccino.couldbook.base.BaseActivity;
import cappuccino.couldbook.core.mananger.AccountM;
import cappuccino.couldbook.data.PhoneNumber;
import cappuccino.couldbook.data.Result;
import cappuccino.couldbook.data.User;
import cappuccino.couldbook.net.NetApi;
import cappuccino.couldbook.net.Network;
import cappuccino.couldbook.ui.Adapter.RecylerInputAdapter;
import rx.Observer;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * Created by Administrator on 2016/9/5.
 */
public class LoginActivity extends BaseActivity implements View.OnClickListener {

    String TAG = "LoginActivity";

    @Override
    protected void init() {

    }

    TextInputLayout tilName, tilPass;
    Button btSubmit,btnRegister;

    @Override
    protected void bindView() {
        setContentView(R.layout.activity_login);
        tilName = (TextInputLayout) findViewById(R.id.til_login_username);
        tilPass = (TextInputLayout) findViewById(R.id.til_login_userpass);
        btSubmit = (Button) findViewById(R.id.btn_login_submit);
        btSubmit.setOnClickListener(this);
        btnRegister = (Button) findViewById(R.id.btn_login_register);
        btnRegister.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_login_submit:
                login();
                break;
            case R.id.btn_login_register:
                toRegister();
                break;
            default:
                break;
        }
    }

    Observer<Result<User>> observer = new Observer<Result<User>>() {

        @Override
        public void onCompleted() {
            Log.v(TAG, "onCompleted");
        }

        @Override
        public void onError(Throwable throwable) {
            Log.v(TAG, "onError");
            Toast.makeText(LoginActivity.this.getApplicationContext(), "We meet some problem ", Toast.LENGTH_SHORT).show();
            Log.v(TAG, throwable.toString());
        }

        @Override
        public void onNext(Result<User> res) {
            Log.v(TAG, "OnNext");
            if (res.status == 1) {
                Toast.makeText(LoginActivity.this.getApplicationContext(), "Welcome back " + res.data.userName, Toast.LENGTH_SHORT).show();
                AccountM accountM = new AccountM(LoginActivity.this);
                accountM.saveAccount(res.data.id);
                Log.v(TAG,"userId" + accountM.getAccout());

               if(toGroups())
                    finish();

            } else
                Toast.makeText(LoginActivity.this.getApplicationContext(), "Username and password wrong", Toast.LENGTH_SHORT).show();
        }
    };

    private void login(String loginname,String loginpass){
        NetApi api = Network.getNetApi();
        api.login(loginname, loginpass).subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(observer);
    }
    private void login() {
        Log.v(TAG, "clicked");
        String userName = tilName.getEditText().getText().toString();
        String userPass = tilPass.getEditText().getText().toString();
        boolean info = true;
        if (userName == null || userName.isEmpty()) {
            tilName.setError("用户名不能为空");
            info = false;
        }
        if (userPass == null || userPass.isEmpty()) {
            tilPass.setError("密码不能为空");
            info = false;
        }
        if (info) {
            login(userName,userPass);
        }
    }

    @Override
    protected boolean toRegister(){
        Intent intent = new Intent(LoginActivity.this,RegisterActivity.class);
        startActivityForResult(intent,Constants.REGISTER);
        return true;
    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        switch (requestCode){
            case Constants.REGISTER:
                if(resultCode == Constants.REGISTER_SUCCESS && data!=null){
                    String username = data.getStringExtra("username");
                    String userpass = data.getStringExtra("userpass");
                    tilName.getEditText().setText(username);
                    tilPass.getEditText().setText(userpass);
                    if(username != null && !username.isEmpty() && userpass!= null && !userpass.isEmpty())
                        login(username,userpass);
                }
                break;
        }
    }
}
